package com.usesion.controller;

import com.usesion.base.entity.JsonResult;
import com.usesion.base.controller.BaseController;
import com.usesion.base.page.PageDataInfo;
import com.usesion.entity.Role;
import com.usesion.entity.User;
import com.usesion.service.RoleService;
import com.usesion.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 * @author XT
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    private String prefix = "system/user";

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user() {
        return prefix + "/user";
    }

    @RequiresPermissions("system:user:list")
    @GetMapping("/list")
    @ResponseBody
    public PageDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getPageData(list);
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("system:user:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Integer userId, Model model) {
        User user = userService.selectUserById(userId);
        List<Role> roles = roleService.selectRolesByUserId(userId);
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return prefix + "/edit";
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("system:user:add")
    @GetMapping("/add")
    public String add(Model model) {
        List<Role> roles = roleService.selectRoleAll();
        model.addAttribute("roles", roles);
        return prefix + "/add";
    }

    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Integer userId, Model model) {
        User user = userService.selectUserById(userId);
        model.addAttribute("user", user);
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:user:resetPwd")
    @PostMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(User user) {
        int rows = userService.resetUserPwd(user);
        if (rows > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    @RequiresPermissions("system:user:remove")
    @RequestMapping("/remove/{userId}")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public JsonResult remove(@PathVariable("userId") Integer userId) {
        User user = userService.selectUserById(userId);
        if (user == null) {
            return JsonResult.failure("用户不存在");
        }
        if (userService.deleteUserById(userId) > 0) {
            return JsonResult.failure();
        }
        return JsonResult.failure();
    }

    @RequiresPermissions("system:user:batchRemove")
    @PostMapping("/batchRemove")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public JsonResult batchRemove(@RequestParam("ids[]") Integer[] ids) {
        int rows = userService.batchDeleteUser(ids);
        if (rows > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    /**
     * 保存用户
     */
    @RequiresPermissions("system:user:save")
    @PostMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public JsonResult save(User user) {
        if (userService.saveUser(user) > 0) {
            return JsonResult.success();
        }
        return JsonResult.failure();
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkUserNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(User user) {
        String uniqueFlag = "0";
        if (user != null) {
            uniqueFlag = userService.checkUserNameUnique(user.getUserName());
        }
        return uniqueFlag;
    }


    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(User user) {
        String uniqueFlag = "0";
        if (user != null) {
            uniqueFlag = userService.checkPhoneUnique(user);
        }
        return uniqueFlag;
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(User user) {
        String uniqueFlag = "0";
        if (user != null) {
            uniqueFlag = userService.checkEmailUnique(user);
        }
        return uniqueFlag;
    }
}